package com.ruoyi.operation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.operation.domain.TFileResult;
import com.ruoyi.operation.domain.bo.TFileResultBo;
import com.ruoyi.operation.domain.vo.TFileResultVo;
import com.ruoyi.operation.mapper.TFileResultMapper;
import com.ruoyi.operation.service.ITFileResultService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文件检测结果Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-22
 */
@RequiredArgsConstructor
@Service
public class TFileResultServiceImpl implements ITFileResultService {

    private final TFileResultMapper baseMapper;

    /**
     * 查询文件检测结果
     */
    @Override
    public TFileResultVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文件检测结果列表
     */
    @Override
    public TableDataInfo<TFileResultVo> queryPageList(TFileResultBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TFileResult> lqw = buildQueryWrapper(bo);
//        ew.getCustomSqlSegment

        System.out.println(lqw.getCustomSqlSegment());
//        Page<TFileResultVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<TFileResultVo> page = new Page<>(pageQuery.getPageNum(),pageQuery.getPageSize());
        IPage<TFileResultVo> result = baseMapper.selectTfileResultList(bo,page);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文件检测结果列表
     */
    @Override
    public List<TFileResultVo> queryList(TFileResultBo bo) {
        LambdaQueryWrapper<TFileResult> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TFileResult> buildQueryWrapper(TFileResultBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TFileResult> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFilegroupid()), TFileResult::getFilegroupid, bo.getFilegroupid());
        lqw.eq(bo.getTestdate() != null, TFileResult::getTestdate, bo.getTestdate());
        lqw.eq(StringUtils.isNotBlank(bo.getResultvalue()), TFileResult::getResultvalue, bo.getResultvalue());
        lqw.eq(StringUtils.isNotBlank(bo.getResultmemo()), TFileResult::getResultmemo, bo.getResultmemo());
        return lqw;
    }

    /**
     * 新增文件检测结果
     */
    @Override
    public Boolean insertByBo(TFileResultBo bo) {
        TFileResult add = BeanUtil.toBean(bo, TFileResult.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文件检测结果
     */
    @Override
    public Boolean updateByBo(TFileResultBo bo) {
        TFileResult update = BeanUtil.toBean(bo, TFileResult.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TFileResult entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文件检测结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean saveBatch(List<TFileResult> arrayList) {
        return baseMapper.insertBatch(arrayList);
    }
}
